package com.maimang.platform.dsp.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.dsp.entity.Ad;
import com.maimang.platform.dsp.service.AdService;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.utils.ExcelUtil;

import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @classname AdController
 * @description <br>广告计划信息操作处理</br>
 * @author  Rocky
 * @date    2019-11-25 11:13:45
 * @email   jintaozhao@qq.com
 * @version 1.0.0
 */
@Controller
@RequestMapping("/dsp/ad")
@Api(value = "广告计划", description = "广告计划 信息操作处理接口")
public class AdController extends BaseController {
    private String prefix = "dsp/ad";

	@Autowired
	private AdService adService;

	@RequiresPermissions("dsp:ad:view")
	@GetMapping()
	public String ad() {
	    return prefix + "/ad";
	}

	/**
	 * 查询广告计划列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告计划列表信息")
	@RequiresPermissions("dsp:ad:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Ad ad) {
		startPage();
        List<Ad> list = adService.selectAdList(ad);
		return getDataTable(list);
	}


	/**
	 * 导出广告计划列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出广告计划列表信息到Excel文件")
	@RequiresPermissions("dsp:ad:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(Ad ad) {
    	List<Ad> list = adService.selectAdList(ad);
        ExcelUtil<Ad> util = new ExcelUtil<Ad>(Ad.class);
        return util.exportExcel(list, "ad");
    }

	/**
	 * 新增广告计划
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增保存广告计划
	 */
	@ApiOperation(value = "新增保存", notes = "参数：Ad")
	@RequiresPermissions("dsp:ad:add")
	@Log(title = "广告计划", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Ad ad) {
		return toResult(adService.insertAd(ad));
	}

	/**
	 * 修改广告计划
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap modelMap) {
		Ad ad = adService.selectAdById(id);
		modelMap.put("ad", ad);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存广告计划
	 */
	@ApiOperation(value = "修改保存", notes = "参数：Ad")
	@RequiresPermissions("dsp:ad:edit")
	@Log(title = "广告计划", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Ad ad) {
		return toResult(adService.updateAd(ad));
	}

	/**
	 * 删除广告计划
	 */
	@ApiOperation(value = "删除对象", notes = "参数：ids，示例： id1,id2,id3，接口会转转为String[]类型")
	@RequiresPermissions("dsp:ad:remove")
	@Log(title = "广告计划", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(adService.deleteAdByIds(ids));
	}

}